package com.studerb.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.Timer;

import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.TaskMonitor;

import com.studerb.Gnusbin;

public class SwingAppStatusPanel extends JPanel {
    private class TaskMonitorListener implements PropertyChangeListener {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if("started".equals(propertyName)){
                if( !busyIconTimer.isRunning()){
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            }
            else if("done".equals(propertyName)){
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            }
            else if("message".equals(propertyName)){
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            }
            else if("progress".equals(propertyName)){
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    }
    private Timer messageTimer;
    private Timer busyIconTimer;
    private Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JSeparator statusPanelSeparator;
    private JProgressBar progressBar;
    private JLabel statusAnimationLabel;
    private JLabel statusMessageLabel;
    private ResourceMap resourceMap;
    private Application swingApp;

    private ApplicationContext swingAppContext;

    public SwingAppStatusPanel() {
        swingApp = Application.getInstance(Gnusbin.class);
        swingAppContext = swingApp.getContext();
        resourceMap = swingAppContext.getResourceMap(SwingAppStatusPanel.class);
        initComponenets();
        initLayout();
        initTimers();
        resourceMap.injectComponents(this);
    }

    private void initComponenets() {
        statusMessageLabel = new JLabel();
        statusAnimationLabel = new JLabel();
        progressBar = new JProgressBar();
        statusPanelSeparator = new JSeparator();
        this.setName("statusPanel");
        statusPanelSeparator.setName("statusPanelSeparator");
        statusMessageLabel.setName("statusMessageLabel");
        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel");
        progressBar.setName("progressBar");
    }

    private void initLayout() {
        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(this);
        this.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(statusPanelLayout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addComponent(statusPanelSeparator,
                        javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE).addGroup(
                        statusPanelLayout.createSequentialGroup().addContainerGap().addComponent(statusMessageLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 226,
                                                        Short.MAX_VALUE).addComponent(progressBar,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(statusAnimationLabel).addContainerGap()));
        statusPanelLayout.setVerticalGroup(statusPanelLayout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        statusPanelLayout.createSequentialGroup().addComponent(statusPanelSeparator,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 2,
                                        javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(
                                        statusPanelLayout.createParallelGroup(
                                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                        statusMessageLabel).addComponent(statusAnimationLabel)
                                                        .addComponent(progressBar,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(3, 3, 3)));
    }

    private void initTimers() {
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for(int i = 0; i < busyIcons.length; i++){
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });

        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);
        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(swingAppContext);
        taskMonitor.addPropertyChangeListener(new TaskMonitorListener());
    }
}
